package com.leadbank.bigdata.mapreduce;

//首先要定义四个泛型的类型
//keyin:  LongWritable    valuein: Text
//keyout: Text            valueout:IntWritable

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    //map方法的生命周期：  框架每传一行数据就被调用一次
    //key :  这一行的起始点在文件中的偏移量
    //value: 这一行的内容
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //拿到一行数据转换为string
        String line = value.toString();
        //将这一行切分出各个单词
        StringTokenizer itr = new StringTokenizer(value.toString());
        //遍历数组，输出<单词，1>
        while(itr.hasMoreElements()){
            String word = itr.nextToken();
            context.write(new Text(word), new IntWritable(1));
            // 为了显示效果而输出Mapper的输出键值对信息
            System.out.println("Mapper输出<" + word + "," + 1 + ">");
        }
    }
}

